﻿using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace SimpleLiteForm.Graphics
{
    /// <summary>
    /// Represents a 2D renderable object with a texture.
    /// </summary>
    public class RenderObject : IDisposable
    {
        /// <summary>
        /// The sprite.
        /// </summary>
        private Sprite _sprite;

        /// <summary>
        /// The texture.
        /// </summary>
        private Texture _texture;

        /// <summary>
        /// Gets or sets the position.
        /// </summary>
        public PointF Position { get; set; }

        /// <summary>
        /// Gets or sets the size.
        /// </summary>
        public SizeF Size { get; set; }

        /// <summary>
        /// A associated tag to store additional data.
        /// </summary>
        public string Tag { get; set; }

        /// <summary>
        /// Gets or sets the tint color.
        /// </summary>
        public Color Color { get; set; }

        /// <summary>
        /// Creates a RenderObject instance.
        /// </summary>
        /// <param name="d3d">The graphics device.</param>
        /// <param name="textureImagePath">The texture image file path.</param>
        public RenderObject(Device d3d, string textureImagePath)
        {
            _sprite = new Sprite(d3d);
            _texture = TextureLoader.FromFile(d3d, textureImagePath);
            Color = Color.White;
        }
        
        /// <summary>
        /// Sets the objects transformation.
        /// </summary>
        /// <param name="mat">The transformation matrix.</param>
        public void setTransform(Matrix mat)
        {
            _sprite.Transform = mat;
        }

        /// <summary>
        /// Renders the object.
        /// </summary>
        public void Draw()
        {
            _sprite.Begin(SpriteFlags.AlphaBlend | SpriteFlags.SortTexture);
            _sprite.Draw2D(_texture, Rectangle.Empty, Size, Position, Color);
            _sprite.End();
        }

        /// <summary>
        /// Renders the object.
        /// </summary>
        /// <param name="color"></param>
        public void Draw3D()
        {
            _sprite.Begin(SpriteFlags.AlphaBlend | SpriteFlags.ObjectSpace);
            Rectangle reshape = new Rectangle(0,0,(int)Size.Width,(int)Size.Height);
            _sprite.Draw(_texture, reshape,new Vector3(Size.Width/2, Size.Height/2, 0), new Vector3(0,0, 0), Color.ToArgb());
            
            _sprite.End();
        }

        /// <summary>
        /// Moves the object.
        /// </summary>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        public void Move(float dx, float dy)
        {
            Position = new PointF(Position.X + dx, Position.Y + dy);
        }

        /// <summary>
        /// Gets the rectangle of the texture.
        /// </summary>
        public Rectangle Rectangle
        {
            get
            {
                return new Rectangle((int)Position.X,
                                     (int)Position.Y,
                                     (int)Size.Width,
                                     (int)Size.Height);
            }
        }

        /// <summary>
        /// Clean up unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (_texture != null)
                _texture.Dispose();

            if (_sprite != null)
                _sprite.Dispose();
        }
    }
}
